<?php

namespace Laoha\Core;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Arr;

class TableController extends AdminController
{
	protected function title()
    {
        return '应用表列表';
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {  
        $grid = new Grid(new LaohaTable());

		$grid->filter(function($filter){
			// 去掉默认的id过滤器
			$filter->disableIdFilter();
			// 在这里添加字段过滤器
			$filter->like('name', 'name');

		});
		$grid->disableRowSelector();
		$grid->disableColumnSelector();


		$grid->column('id', 'ID');
        $grid->column('name', '表名');
		/*
		$grid->column('id', '管理')->display(function ($id) {
			return "<a href='".route('admin.laoha.cate.index', ['table_id' => $id])."' style='color:blue'>分类管理</span>";
		});
		*/
        $grid->column('model', '模型');
		$grid->column('allow_cate', '允许分类')->display(function ($allow_cate) {
			return $allow_cate ?  '是':'否';
		});
		$grid->actions(function (Grid\Displayers\Actions $actions) {
			$actions->disableView();
		});

        return $grid;
    }

   

	/**
     * Make a form builder.
     *
     * @return Form
     */
    public function form()
    {

        $form = new Form(new LaohaTable());

        $form->display('id', 'ID');

        
        $form->text('name', '表名')
			->creationRules('required|unique:laoha_tables,name|regex:/^[A-Za-z][A-Za-z0-9_]+$/',['regex' => '名称必须是英文开头且只包含字母、数字、下划线',])
			->updateRules('required|unique:laoha_tables,name,{{id}}|regex:/^[A-Za-z][A-Za-z0-9_]+$/',['regex' => '名称必须是英文开头且只包含字母、数字、下划线',]);
		$form->text('model', '模型')->rules('required');
		$form->switch('allow_cate', '允许分类')->help('表中必须包含cate_id字段才有效');
		$form->textarea('items', '特色项目')->help('一行一个，例:首页幻灯,精华等');;

        return $form;

    }

    public function destroy($id)
    {
        $cateModel = new LaohaCate();
		$cate = $cateModel->where('table_id',$id)->first();
		if($cate){
            $response = [
                'status'  => false,
                'message' => '请先删除此表下的所有分类项目',
            ];
			return response()->json($response);
		}
		return $this->form()->destroy($id);
    }
}
